package com.jackiechan.day17;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

public class Test1 {
    public static void main(String[] args) throws ClassNotFoundException {
//        method();
//        method2();
//        method3();
//        method4();
    }
    // 获取所有成员变量
    private static void method4() {
        Class c = Student.class;
        System.out.println(c.getFields().length);
        for (Field field :c.getFields()) {
            System.out.print(field.getName()+" ");
            System.out.println(field.getType());
        }
        System.out.println("*********");
        System.out.println(c.getDeclaredFields().length);
        for (Field field :c.getDeclaredFields()) {
            System.out.print(field.getName()+" ");
            System.out.println(field.getType());
        }

    }

    // 获取所有方法
    private static void method3() {
        Class c = Student.class;
        System.out.println(c.getMethods().length);
        for (Method method :c.getMethods()) {
            System.out.print(method.getName()+"  ");
//            System.out.print(method.getParameterCount()+"  ");
//            System.out.println(Arrays.toString(method.getParameterTypes()));
        }
        System.out.println("\n*********");
        System.out.println(c.getDeclaredMethods().length);
        for (Method method :c.getDeclaredMethods()) {
            System.out.print(method.getName()+"  ");
        }
        System.out.println("\n+++++++++");
        System.out.println(c.getSuperclass());


    }

    // 获取所有public构造方法
    private static void method2() {
        Class c = Student.class;
        // default 和 private 构造方法不能获取
        System.out.println(c.getConstructors().length + " 个构造方法如下:");
        for (Constructor constructor : c.getConstructors()) {
            System.out.println("  " + constructor);
            System.out.print("     " + constructor.getParameterTypes().length + " 个参数:  ");
            System.out.println(Arrays.toString(constructor.getParameterTypes()));
        }
        System.out.println("**********");
        // default 和 private 构造方法也能获取
        System.out.println(c.getDeclaredConstructors().length + " 个构造方法如下:");
        for (Constructor constructor : c.getDeclaredConstructors()) {
            System.out.println("  " + constructor);
            System.out.print("     " + constructor.getParameterTypes().length + " 个参数:  ");
            System.out.println(Arrays.toString(constructor.getParameterTypes()));
        }
    }

    // 获取对象
    private static void method() throws ClassNotFoundException {
//        Class a = Class.forName("com.jackiechan.day17.Student");
//        Class b = new Student().getClass();
        Class c = Student.class;
        System.out.println(c.getPackage().getName());
        System.out.println(c.getSimpleName());
        System.out.println(c.getName());
        System.out.println();
        System.out.println(Arrays.toString(c.getFields()));
        System.out.println(Arrays.toString(c.getMethods()));
        for (int i = 0; i < c.getMethods().length; i++) {
            System.out.println(c.getMethods()[i]);
        }

    }

}
